package com.ctg.itrdc.k8sconsole.control;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.k8sconsole.service.ILogService;
import com.ctg.itrdc.paspsdk.common.utils.json.JSONUtils;

/**
 * 
 * @description: pod与容器的日志  查询
 * @version: v1.0.0 
 * @author: Piao.Wu
 * 
 * Modification History:
 * Date               Author      Version     Description
 * -----------------------------------------------------------------
 * 2016年8月23日 下午8:03:22     Piao.Wu       v1.0.0        create
 *
 */
@Controller
@RequestMapping("/log")
public class LogController {

	@Resource
	private ILogService logService;
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 
	 * @Description: 获取具体pod的log信息
	 * @author Piao.Wu 2016年9月7日 下午5:31:41
	 * @param clusterCode
	 * @param namespace
	 * @param podname
	 * @return
	 * CommonResult
	 *
	 */
	// TODO 无用接口，最后可去掉
	@RequestMapping(value = "/queryPodLog", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryPodLog(@RequestParam("clusterCode") String clusterCode
			,@RequestParam("namespace") String namespace
			,@RequestParam("perpage") String perpage
			,@RequestParam("page") String page
			,@RequestParam("podname") String podname) {
		
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = logService.getPodLog(clusterCode,namespace, podname,perpage,page);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("服务器出现异常！",e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	/**
	 * 
	 * @Description: 获取具体pod下的容器log信息
	 * @author Piao.Wu 2016年9月7日 下午5:31:29
	 * @param clusterCode
	 * @param namespace
	 * @param podname
	 * @param containername
	 * @return
	 * CommonResult
	 *
	 */
	@RequestMapping(value = "/queryContainerLog", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryContainerLog(@RequestParam("clusterCode") String clusterCode
			 ,@RequestParam("namespace") String namespace
			 ,@RequestParam("podname") String podname
			 ,@RequestParam("perpage") String perpage
			 ,@RequestParam("page") String page
			 ,@RequestParam("containername") String containername) {
		
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = logService.getContainerLog(clusterCode,namespace, podname, containername,perpage,page);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("服务器出现异常！",e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	
}
